package cn.wang.page.mq;

import cn.wang.page.service.PageService;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class ItemListener {
    @Autowired
    private PageService pageService;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "page.item.insert.queue", durable = "true"),
            exchange = @Exchange(name = "ly.item.exchange", type = ExchangeTypes.TOPIC),
            key = {"item.insert", "item.update"}
    ))
    public void listUpdateOrCreate(Long spuId) {
        if (spuId == null) {
            return;
        }
        pageService.createTemplate(spuId);
    }


    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "page.item.delete.queue", durable = "true"), // name指定队列名称, 没有会创建,durable,对队列进行持久化
            exchange = @Exchange(name = "ly.item.exchange", type = ExchangeTypes.TOPIC), // name指定和那个交换器进行绑定
            key = {"item.delete"}
    ))
    public void listDelete(Long spuId) {
        if (spuId == null) {
            return;
        }
        pageService.DeleteTemplate(spuId);
    }
}